###
#set table = $tabdict.table
#set Table = $tabdict.Table
#set pk = $tabdict.pk
#set PTable = $tabdict.PTable
#set propertys = $tabdict.propertys
#set type = $tabdict.type
#set jtable = $tabdict.jtable
package com.kylin.module.${type}.business;

import org.apache.log4j.Logger;

import java.util.Date;
import com.kylin.common.exception.BusinessException;
import com.kylin.common.paginate.DisplayPage;
import com.kylin.common.paginate.PageManager;

import com.kylin.hibernate.BaseHibernateDAO;

import com.kylin.common.paginate.Paging;

import com.kylin.module.${type}.forms.${Table}Form;

import com.kylin.module.${type}.orm.${Table};
import com.kylin.module.${type}.orm.${Table}DAO;

import com.kylin.module.user.orm.User;

#for  $cl in $PTable
#if $cl.class == 'Section'
import com.kylin.module.common.orm.${cl.class}DAO;
import com.kylin.module.common.orm.${cl.class};
#else if $cl.seq == '' 
import com.kylin.module.dictionarys.orm.${cl.class}DAO;
import com.kylin.module.dictionarys.orm.${cl.class};
#else

// ${cl.class} $cl.seq
#end if
#end for

public class ${Table}Blogic{
	protected static Logger log = Logger.getLogger(${Table}Blogic.class);
	
	public void save(User user, ${Table}Form ${table.lower()}form){
		
		log.info("in ${Table}.save by "+user.getUsername());
		log.info("in ${Table}.save, form.toString(): "+${table.lower()}form.toString());
		//try{
			log.info("get last 10 registers"+user.getUsername());
			
			${Table} model =  new ${Table}();
			${Table}DAO dao = new ${Table}DAO();

#set $auto = {'language':'1','createdby':'1','createdon':'1','lastupdatedby':'1','lastupdatedon':'1','active':'1','ordering':'1','hits':'1'}			
#for $field in $propertys
#if $field.name not in $auto
			model.set${field.name.capitalize()}(${table.lower()}form.get${field.name.capitalize()}());
#end if
#end for

#for  $cl in $PTable
#set $onename ="%s%s" %($cl.name, $cl.seq)
#if $onename != 'user'
			/*  set ${cl.name.capitalize()}  */
			if (${table.lower()}form.get${cl.name.capitalize()}id() != null && ${table.lower()}form.get${cl.name.capitalize()}id() != 0){
	 			${cl.class}DAO  ${cl.name}dao =  new ${cl.class}DAO();
				${cl.class} ${cl.name}${cl.seq} =  ${cl.name}dao.findById(${table.lower()}form.get${cl.name.capitalize()}id());
				model.set${cl.name.capitalize()}${cl.seq}( ${cl.name}${cl.seq});
			}
#else
			/* user in session */
			model.setUser(user);
#end if			
#end for

			model.setCreatedon(new Date());
			model.setActive((short)1);
			model.setHits(0);
			model.setCreatedby(user.getUsername());
			
			dao.save(model);
		//}
		//catch(BusinessException e){
		//}
	}


	public void update(User user, ${Table}Form ${table.lower()}form){
		
		log.info("in ${Table}.updated by "+user.getUsername());
		log.info("in ${Table}.updated, form.toString(): "+${table.lower()}form.toString());
		try{
			log.info("get last 10 registers"+user.getUsername());
			
			${Table} model =  (${Table})BaseHibernateDAO.findById(${Table}.class, ${table.lower()}form.getId());
			${Table}DAO dao = new ${Table}DAO();

#set $auto = {'language':'1','createdby':'1','createdon':'1','lastupdatedby':'1','lastupdatedon':'1','active':'1','ordering':'1','hits':'1'}			
#for $field in $propertys
#if $field.name not in $auto
			model.set${field.name.capitalize()}(${table.lower()}form.get${field.name.capitalize()}());
#end if
#end for

#for  $cl in $PTable
#set $onename ="%s%s" %($cl.name, $cl.seq)
#if $onename != 'user'
			/*  set ${cl.name.capitalize()}  */
			if (${table.lower()}form.get${cl.name.capitalize()}id() != null && ${table.lower()}form.get${cl.name.capitalize()}id() != 0){
	 			${cl.class}DAO  ${cl.name}dao =  new ${cl.class}DAO();
				${cl.class} ${cl.name}${cl.seq} =  ${cl.name}dao.findById(${table.lower()}form.get${cl.name.capitalize()}id());
				model.set${cl.name.capitalize()}${cl.seq}( ${cl.name}${cl.seq});
			}
#else
			/* user in session */
			model.setUser(user);
#end if			
#end for

			model.setLastupdatedon(new Date());
			//model.setActive((short)1);
			model.setHits(1 + model.getHits());
			model.setCreatedby(user.getUsername());
			dao.saveOrUpdate(model);
			//dao.save(model);
		}
		catch(BusinessException e){
		}
	}


	
	public Paging show(Integer limitstart, Integer limit){
		Paging page = new Paging();
		${Table}DAO fdao = new  ${Table}DAO();
		
		page.setTotal(fdao.getTotal());
		page.setList(fdao.getList(limitstart, limit));
		page.setStart(limitstart);
		page.setLimit(limit);
		
		return page;		
	}	
	

}
